package com.example.AIstudy.controller;

import com.example.AIstudy.DTO.RecordDTO;
import com.example.AIstudy.domain.*;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.RecordDetailService;
import com.example.AIstudy.service.UserSkillScoreService;
import com.example.AIstudy.util.MyThread;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/recordDetail")
@Api(tags = "用户做题详情的管理API")
public class RecordDetailController {

    @Autowired
    RecordDetailService recordDetailService;

    @Autowired
    AnalyseController analyseController;

    @Autowired
    UserSkillScoreService userSkillScoreService;

    //新增一个用户做套题的记录
    @PostMapping("")
    @ApiOperation("新增用户答题详情")
    void insertRecord(@ApiParam("答题详情")@RequestBody RecordDTO recordDTO)throws AIstudyException {
        RecordDetail detail=recordDTO;
//        System.out.println(recordDTO.toString());
        recordDetailService.insertRecordDetail(recordDTO);
    }

    //更新做题的记录
    @PutMapping("")
    @ApiOperation("更新用户答题详情")
    public void updateRecord(@ApiParam("答题详情")@RequestBody RecordDTO RecordDto) throws AIstudyException{
        System.out.println(RecordDto.toString());
        recordDetailService.updateRecord(RecordDto);
    }

    //分页获取做题的记录
    @GetMapping("")
    @ApiOperation("根据条件获取用户答题详情")
    public List<RecordDTO> findRecord(@ApiParam("试卷ID")@RequestParam(value = "exerciseID",required = false) Integer exerciseID,
                                      @ApiParam("最少分")@RequestParam(value = "minScore",required = false) Integer minScore,
                                      @ApiParam("最高分")@RequestParam(value = "maxScore",required = false) Integer maxScore){

        Map<String,Object> condition=new HashMap<>();
        if(exerciseID!=null) {
            condition.put("exerciseID",exerciseID);
        }
        if(minScore!=null) {
            condition.put("minScore",minScore);
        }
        if(maxScore!=null) {
            condition.put("maxScore",maxScore);
        }
        return recordDetailService.findRecord(condition);
    }

    @PostMapping("/submitexe")
    @ApiOperation("获取推荐的题目后，进行答题并上传，获取解析结果")
    public Exercise insertRecordBySubmitExe(@RequestBody Exercise exercise) throws AIstudyException {
        String category=exercise.getCategory();
        Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
        if(category.equals("yuedu")){
            //增加一个历史做题列表
//            List<RecordDetail>records=new ArrayList<>();
            List<Exercise>exercises=exercise.getSonExercise();
            int size=exercises.size();
            RecordDetail first=new RecordDetail();
            StringBuilder builder=new StringBuilder();
            for(int i=0;i<size;i++){
                RecordDetail record=new RecordDetail();
                record.setInsertTime(currentTimestamp);
                //获取分析结果
                CuoTiJi tmpCuotiji=new CuoTiJi();
                Exercise tmpexe=exercises.get(i);
                //封装错题
                tmpCuotiji.setContent(exercise.getContent());
                tmpCuotiji.setCategory(exercise.getCategory());
                tmpCuotiji.setProblem(tmpexe.getProblem());
                tmpCuotiji.setChoice1(tmpexe.getChoice1());
                tmpCuotiji.setChoice2(tmpexe.getChoice2());
                tmpCuotiji.setChoice3(tmpexe.getChoice3());
                tmpCuotiji.setChoice4(tmpexe.getChoice4());
                tmpCuotiji.setAnswer(tmpexe.getAnswer());
                tmpCuotiji.setStuAnswer(tmpexe.getStuAnswer());
                String aiComment= (String) analyseController.analyseUserAnswer(tmpCuotiji);
                //封装分析结果
                record.setAiComment(aiComment);
                record.setExerciseID(tmpexe.getId());
                record.setStuAnswer(tmpexe.getStuAnswer());
                record.setStuID(exercise.getStuID());
                if(tmpexe.getStuAnswer().equals(tmpexe.getAnswer())){
                    record.setRight(true);
                    tmpexe.setRight(true);
                    //正确后的评分
                    record.setStuScore(tmpexe.getScore());
                    tmpexe.setStuScore(tmpexe.getScore());
                }
                //更新exe的信息并且返回
                tmpexe.setAiComment(aiComment);
                //将记录封装在列表中
//                records.add(record);
                recordDetailService.insertRecordDetail2(record);
                //如果不是第一个 则保存id信息
                if(i!=0){
                    builder.append(record.getId());
                    builder.append(",");
                }else{
                    //暂存第一个对象
                    first=record;
                }
            }
            //更新第一个对象
            String son=builder.toString();
            first.setSonRecordID(son.substring(0,son.length()-1));
            recordDetailService.updateById(first);

        }
        else {
            RecordDetail record=new RecordDetail();
            record.setInsertTime(currentTimestamp);
            record.setStuAnswer(exercise.getStuAnswer());
            record.setExerciseID(exercise.getId());
            record.setStuID(exercise.getStuID());
            //封装错题进行分析
            CuoTiJi tmpCuotiji=new CuoTiJi();
            tmpCuotiji.setCategory(exercise.getCategory());
            tmpCuotiji.setAnswer(exercise.getAnswer());
            tmpCuotiji.setStuID(exercise.getStuID());
            tmpCuotiji.setStuAnswer(exercise.getStuAnswer());
            tmpCuotiji.setContent(exercise.getContent());
            tmpCuotiji.setScore(exercise.getScore());
            if(category.equals("zuowen")){
                //获取到作文的分析结果，并且进行封装
                CompositionDetail detail= (CompositionDetail) analyseController.analyseUserAnswer(tmpCuotiji);
//                cuoTiJi.setTransCommentID(detail.getId());
                record.setCompCommentID(detail.getId());
                exercise.setTransCommentID(detail.getId());
                exercise.setCompositionDetail(detail);
                record.setStuScore(detail.getTotalScore());
                exercise.setStuScore(detail.getTotalScore());
            }else if(category.equals("fanyi")){
                //获取到翻译的分析结果，并且进行封装
                TranslateDetail detail= (TranslateDetail) analyseController.analyseUserAnswer(tmpCuotiji);
//                cuoTiJi.setTransCommentID(detail.getId());
                record.setTransCommentID(detail.getId());
                exercise.setTranslateDetail(detail);
                exercise.setTransCommentID(detail.getId());
                record.setStuScore(detail.getTotalScore());
                exercise.setStuScore(detail.getTotalScore());
            }else if(category.equals("xuanze")){
                //封装选择
                tmpCuotiji.setProblem(exercise.getProblem());
                tmpCuotiji.setChoice1(exercise.getChoice1());
                tmpCuotiji.setChoice2(exercise.getChoice2());
                tmpCuotiji.setChoice3(exercise.getChoice3());
                tmpCuotiji.setChoice4(exercise.getChoice4());
                String aiComment= (String) analyseController.analyseUserAnswer(tmpCuotiji);
                record.setAiComment(aiComment);
                if(exercise.getStuAnswer().equals(exercise.getAnswer())){
                    record.setRight(true);
                    exercise.setRight(true);
                    record.setStuScore(exercise.getScore());
                    exercise.setStuScore(exercise.getScore());
                }
                exercise.setAiComment(aiComment);
            }
            recordDetailService.save(record);
        }
        //更新用户的信息，开启一个新线程
        MyThread thread =new MyThread(userSkillScoreService);
        thread.stuID=exercise.getStuID();
        thread.start();
        //返回封装后的结果
        return  exercise;
    }
}
